//
//  AbilityViewController.m
//  LOL_JewelBox
//
//  Created by dllo on 15/4/25.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "AbilityViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "UIImageView+WebCache.h"
#import "ToolsModel.h"
#import "ToolsView.h"
#import "DetailModel.h"
#import "DetailNewsCell.h"
#import "DetailCollectionCell.h"
#import "DetailAbilityViewController.h"
@interface AbilityViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic, retain)ToolsView *toolsView;
@property(nonatomic, retain)UICollectionView *collectionView;

@property(nonatomic, retain)NSMutableArray *transformArr;
@property(nonatomic, retain)NSMutableArray *abilityArr;

@end

@implementation AbilityViewController

- (void)dealloc
{
    [_toolsView release];
    [_collectionView release];
    [_transformArr release];
    [_abilityArr release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.title = @"召唤师技能";
        self.transformArr = [NSMutableArray array];
        self.abilityArr = [NSMutableArray array];
        
    }
    return self;
}

- (void)abilityData
{
    AFHTTPRequestOperationManager *managerAbility = [AFHTTPRequestOperationManager manager];
    [managerAbility GET:@"http://lolbox.duowan.com/phone/apiSumAbility.php?OSType=iOS7.1.1&v=70" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSMutableArray *arr = responseObject;
        for (NSInteger i = 0; i < arr.count; i++) {
            NSMutableDictionary *dic = arr[i];
            DetailModel *abilityModel = [[DetailModel alloc] init];
            [abilityModel setValuesForKeysWithDictionary:dic];
            [self.transformArr addObject:abilityModel];
            [abilityModel release];
            
        }
        [self.collectionView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self abilityData];
    
    // 铺设自定义View
    self.toolsView = [[ToolsView alloc] initWithFrame:CGRectMake(0, 0, VIEWWIDTH, VIEWHEIGHT)];
    [self.view addSubview:self.toolsView];
    self.toolsView.backgroundColor = [UIColor whiteColor];
    [_toolsView release];
    
    // 设置collectionView
    // 创建一个layout
    UICollectionViewFlowLayout *flovLayout = [[UICollectionViewFlowLayout alloc] init];
    // 图片大小
    flovLayout.itemSize = CGSizeMake(VIEWWIDTH / 5.357, VIEWHEIGHT / 8.893);
    // 最小行列间距
    flovLayout.minimumInteritemSpacing = 1;
    flovLayout.minimumLineSpacing = (VIEWWIDTH / 5.357) / 4;
    // 设置item的边框
    flovLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10,10);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(5, 5, VIEWWIDTH - 10, VIEWHEIGHT - 96 - 30 ) collectionViewLayout:flovLayout];
    // 设置代理人
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor colorWithRed:218 / 255.0 green:218 / 255.0 blue:218 / 255.0 alpha:1];
    [self.toolsView addSubview:self.collectionView];
    [_collectionView release];
    
    // 注册方法
    [self.collectionView registerClass:[DetailCollectionCell class] forCellWithReuseIdentifier:@"reuse"];
    
    for (NSMutableDictionary *dic in self.abilityArr) {
    [self.transformArr addObject:dic];
    }
    [self.collectionView reloadData];
    
}

// collectionView的两个协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.transformArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    DetailCollectionCell *abilityCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    DetailModel *abilityModel = self.transformArr[indexPath.row];
    abilityCell.detailName.text = abilityModel.name;
    
    NSString *png = [NSString stringWithFormat:@"http://img.lolbox.duowan.com/spells/png/%@.png", abilityModel.id];
    NSURL *url = [NSURL URLWithString:png];
    [abilityCell.detailPng sd_setImageWithURL:url];

    
    return abilityCell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DetailAbilityViewController *detailAbility = [[DetailAbilityViewController alloc] init];
    DetailModel *abilityModel = self.transformArr[indexPath.row];
    detailAbility.detailAbilityid = abilityModel.id;
    detailAbility.detailAbilityDes = abilityModel.des;
    detailAbility.detailAbilityName = abilityModel.name;
    detailAbility.detailAbilityLevel = abilityModel.level;
    detailAbility.detailAbilityCooldown = abilityModel.cooldown;
    detailAbility.detailAbilityTips = abilityModel.tips;
    [self.navigationController pushViewController:detailAbility animated:YES];
    [detailAbility release];
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
